import 'dart:math';

void main() {
  // if 语句
  var number = Random().nextInt(10);
  if (number >= 5) {
    print('生成数字$number 大于等于5');
  } else {
    print('生成数字$number 小于5');
  }
  number = Random().nextInt(10);
  print('${number >= 5 ? '生成数字$number 大于等于5' : '生成数字$number 小于5'}');

  // switch 语句
  int state = Random().nextInt(4);
  switch (state) {
    case 0:
      print('当前状态为待付款');
      break;
    case 1:
      print('当前状态为待发货');
      break;
    case 2:
      print('当前状态为待收货');
      break;
    case 3:
      print('当前状态为待评价');
      break;
    default:
      print('当前状态未知');
  }

  // while 语句
  num count = 1;
  while (count <= 10) {
    print('第 $count 次循环');
    count++;
  }

  // for 循环
  const arr = ['apple', 'banana', 'orange', 'grape', 'pear'];
  for (var item in arr) {
    print('数组元素为 $item');
  }

  // 循环遍历 计算选中的金额
  List allGoodsList = [
    {'name': 'apple', 'price': 11.0, 'count': 5, 'selected': true},
    {'name': 'banana', 'price': 10.5, 'count': 2, 'selected': true},
    {'name': 'orange', 'price': 14.5, 'count': 4, 'selected': true},
    {'name': 'grape', 'price': 12.0, 'count': 3, 'selected': false},
  ];

  // 计算选中的金额
  // 方法一
  double selectedAmount = allGoodsList.fold(0.0, (prev, curr) {
    if (curr['selected']) {
      return prev + (curr['price'] * curr['count']);
    } else {
      return prev;
    }
  });

  // double selectedAmount = 0.0;
  // 方法二
  // for (var item in allGoodsList) {
  //   if (item['selected']) {
  //     selectedAmount += (item['price'] * item['count']);
  //   }
  // }
  // 方法三
  // for (var i = 0; i < allGoodsList.length; i++) {
  //   if (allGoodsList[i]['selected']) {
  //     selectedAmount += (allGoodsList[i]['price'] * allGoodsList[i]['count']);
  //   }
  // }
  print('选中的金额为 $selectedAmount');
}
